<template>
  <el-dialog width="40%" :title="dialogTitle" v-model="dialogVisible" modal draggable @close="closeDialog"
    :close-on-click-modal="false">
    <el-form class="dialog-form" :model="formData" ref="readerForm" label-position="right" label-width="100px"
      size="medium" @submit.native.prevent>
      <el-form-item label="读写器:" prop="deviceId">
        <el-select v-model="formData.deviceId" placeholder="请选择读写器" class="full-width-input" clearable
          @change="changeReader">
          <el-option v-for="(item, index) in readerOptions" :key="index" :label="item.label" :value="item.value"
            :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="读写器状态:" prop="state">
        {{ formData.state }}
      </el-form-item>
      <el-form-item label="RFID:" prop="rfid">
        {{ formData.rfid }}
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog" size="mini">取 消</el-button>
      <el-button v-if="type === 'replace'" type="primary" @click="replaceLabel" size="mini">更 换</el-button>
      <el-button v-if="type === 'register'" type="primary" @click="getRfid" size="mini">读 卡</el-button>
      <el-button v-if="type === 'register'" type="primary" @click="registerLabel" size="mini">注 册</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { dropDown, getInfo } from '@/api/bas/rfidReader'
import { archiveReplace, archiveRegister, boxReplace, boxRegister } from '@/api/rfid/labelReplace'
import { queryByRfid } from '@/api/rfid/label'
import axios from 'axios'
import common from '@/utils/common'
import { ElNotification } from 'element-plus'

export default {
  name: 'labelReplaceDialog',
  data() {
    return {
      type: '',
      labelType: '',
      labelInfo: {},
      labelId: '',
      dialogTitle: '',
      dialogVisible: false,
      formData: {
        deviceId: '',
        state: '未选择读写器',
        rfid: '',
      },
      readerInfo: {},
      readerOptions: [],
    }
  },
  methods: {
    getReader() {
      dropDown()
        .then((res) => {
          if (res.data.code === 200) {
            this.readerOptions = res.data.data
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    show(row, type, labelType) {
      this.type = type
      this.labelType = labelType
      this.labelInfo = row
      if (type === 'register') {
        this.dialogTitle = '标签注册'
        this.formData.rfid = ''
      } else if (type === 'replace') {
        this.dialogTitle = '标签更换'
        this.formData.rfid = row.rfid
      }
      this.labelId = row.labelId
      this.getReader()
      this.dialogVisible = true
    },
    changeReader(value) {
      getInfo(value)
        .then((res) => {
          if (res.data.code === 200) {
            this.readerInfo = res.data.data
            this.takeOn(res.data.data.deviceIp, res.data.data.devicePort)
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    takeOn(ip, port) {
      const that = this
      let url = 'http://' + ip + ':' + port + '/takeon'
      axios
        .get(url, { timeout: 10000 })
        .then((res) => {
          if (res.data.search('ok') !== -1) {
            that.formData.state = '正常'
          } else {
            that.formData.state = '离线'
          }
          this.$forceUpdate()
        })
        .catch((error) => {
          that.formData.state = '离线'
          ElNotification({
            message: '连接失败',
            type: 'warning',
          })
        })
    },
    replaceLabel() {
      if (this.formData.state === '正常') {
        let url =
          'http://' + this.readerInfo.deviceIp + ':' + this.readerInfo.devicePort + '/getrfid'
        axios
          .get(url, { timeout: 1000 })
          .then((res) => {
            const a = eval('(' + res.data + ')').result
            if (a.length === 0) {
              ElNotification({
                message: '请放置标签',
                type: 'warning',
              })
            } else if (a.length > 1) {
              ElNotification({
                message: '只能放置一个标签',
                type: 'warning',
              })
            } else {
              queryByRfid(a[0].ID).then((res) => {
                if (res.data.code === 200) {
                  this.setRfid()
                } else {
                  ElNotification({
                    message: res.data.message,
                    type: 'warning',
                  })
                }
              })
            }
          })
          .catch((error) => {
            this.$message.warning('连接失败')
          })
      } else if (this.formData.state === '离线') {
        ElNotification({
          message: '读写器离线',
          type: 'warning',
        })
      } else {
        ElNotification({
          message: '请选择读写器',
          type: 'warning',
        })
      }
    },
    replace() {
      const param = {
        labelId: this.labelInfo.labelId,
        oldRfid: this.labelInfo.rfid,
        newRfid: this.labelInfo.rfid,
      }
      if (this.labelType === 'archive') {
        archiveReplace(param)
          .then((res) => {
            if (res.data.code === 200) {
              this.$message.success('更换成功')
              this.closeDialog()
              this.$emit('ok')
            } else {
              this.$message.warning(res.data.message)
            }
          })
          .catch((err) => {
            this.$message.error(err)
          })
      } else if (this.labelType === 'box') {
        boxReplace(param)
          .then((res) => {
            if (res.data.code === 200) {
              this.$message.success('更换成功')
              this.closeDialog()
              this.$emit('ok')
            } else {
              this.$message.warning(res.data.message)
            }
          })
          .catch((err) => {
            this.$message.error(err)
          })
      }
    },
    registerLabel() {
      if (this.formData.state === '正常') {
        if (this.formData.rfid === '') {
          ElNotification({
            message: '请点击读卡获取rfid',
            type: 'warning',
          })
          return
        }
        this.register()
      } else if (this.formData.state === '离线') {
        ElNotification({
          message: '读写器离线',
          type: 'warning',
        })
      } else {
        ElNotification({
          message: '请选择读写器',
          type: 'warning',
        })
      }
    },
    register() {
      const param = {
        labelId: this.labelInfo.labelId,
        oldRfid: this.labelInfo.rfid,
        newRfid: this.formData.rfid,
      }
      if (this.labelType === 'archive') {
        archiveRegister(param)
          .then((res) => {
            if (res.data.code === 200) {
              this.$message.success('注册成功')
              this.closeDialog()
              this.$emit('ok')
            } else {
              this.$message.warning(res.data.message)
            }
          })
          .catch((err) => {
            this.$message.error(err)
          })
      } else if (this.labelType === 'box') {
        boxRegister(param)
          .then((res) => {
            if (res.data.code === 200) {
              this.$message.success('注册成功')
              this.closeDialog()
              this.$emit('ok')
            } else {
              this.$message.warning(res.data.message)
            }
          })
          .catch((err) => {
            this.$message.error(err)
          })
      }
    },
    setRfid() {
      const that = this
      let url =
        'http://' +
        that.readerInfo.deviceIp +
        ':' +
        that.readerInfo.devicePort +
        '/setrfid/' +
        that.formData.rfid
      axios
        .get(url, { timeout: 1000 })
        .then((res) => {
          if (res.data.search('ok') !== -1) {
            this.replace()
          } else {
            ElNotification({
              message: '标签更换失败,读写器只能放置一个标签',
              type: 'warning',
            })
          }
        })
        .catch((error) => {
          ElNotification({
            message: '连接失败',
            type: 'warning',
          })
        })
    },
    takeOff() {
      const that = this
      let url = 'http://' + that.readerInfo.deviceIp + ':' + that.readerInfo.devicePort + '/takeoff'
      axios
        .get(url, { timeout: 1000 })
        .then((res) => {
          if (res.data.search('ok') !== -1) {
            this.$message.warning('连接关闭')
          } else {
            ElNotification({
              message: '读写器关闭失败',
              type: 'warning',
            })
          }
        })
        .catch((error) => {
          ElNotification({
            message: '连接失败',
            type: 'warning',
          })
        })
    },
    getRfid() {
      const that = this
      if (that.formData.state === '正常') {
        let url =
          'http://' + that.readerInfo.deviceIp + ':' + that.readerInfo.devicePort + '/getrfid'
        axios
          .get(url, { timeout: 1000 })
          .then((res) => {
            const a = eval('(' + res.data + ')').result
            if (a.length === 0) {
              if (that.formData.rfid !== '') {
                ElNotification({
                  message: '请放置新的标签',
                  type: 'warning',
                })
              } else {
                ElNotification({
                  message: '请放置标签',
                  type: 'warning',
                })
              }
            } else if (a.length > 1) {
              ElNotification({
                message: '只能放置一个标签',
                type: 'warning',
              })
            } else {
              that.formData.rfid = a[0].ID
            }
          })
          .catch((error) => {
            ElNotification({
              message: '连接失败',
              type: 'warning',
            })
          })
      } else if (that.formData.state === '离线') {
        ElNotification({
          message: '读写器离线',
          type: 'warning',
        })
      } else {
        ElNotification({
          message: '请选择读写器',
          type: 'warning',
        })
      }
    },
    closeDialog() {
      if (this.formData.state === '正常') {
        this.takeOff()
      }
      this.$refs['readerForm'].resetFields()
      this.dialogVisible = false
    },
  },
}
</script>

<style scoped></style>
